package com.SFA.OrderManagement;

import java.lang.ref.SoftReference;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.FrameWork.Common.BitmapManager;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;
import com.FrameWork.Utils.Utils;
import com.SFA.R;
import com.SFA.OrderManagement.ProductListAdapter.ViewHoder;
import com.SFA.OrderManagement.ProductView.ISelectProduct;
import com.SFA.ProductShow.ProductPhotoList;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class HorizationalProductAdapter extends BaseAdapter implements OnClickListener {
	private Context context;
	private List<ProductEntity> list;
	protected LayoutInflater inflater;

	public ISelectProduct OnSelectProduct;


	public static ExecutorService pool= Executors.newCachedThreadPool(Executors.defaultThreadFactory());
	public HorizationalProductAdapter(Context context, List<ProductEntity> list) {
		this.context=context;
		this.list =list;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if(list == null)
			return 0;
		return list.size();
	}

	@Override
	public ProductEntity getItem(int position) {
		if(list == null || list.size()<=position)
			return null;
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public interface ISelectProduct
	{
		void Select(ProductEntity entity);
	}



	@Override
	public void onClick(View v) {
		ProductEntity entity = (ProductEntity)v.getTag();
		Button btnSelect= (Button)v;
		entity.Checked =!entity.Checked;
		if(entity.Checked)
		{
			btnSelect.setText(R.string.CANCEL);
			btnSelect.setBackgroundResource(R.drawable.mydate_ok);
		}
		else
		{
			btnSelect.setText(R.string.label_select);
			btnSelect.setBackgroundResource(R.drawable.mydate_cancel);
		}

		//触发通知事件
		if(OnSelectProduct != null)
		{
			OnSelectProduct.Select(entity);
		}
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null)
		{

			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.product_view, null);
			holder.btnSelect = (Button) convertView.findViewById(R.id.btnSelect);
			holder.btnImage = (ImageView) convertView.findViewById(R.id.btnImage);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
			holder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
			holder.btnImage.setDrawingCacheEnabled(false);
			holder.btnSelect.setOnClickListener(this);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		ProductEntity entity = this.getItem(position);
		holder.entity = entity;
		holder.btnSelect.setTag(entity);
		if(entity.Checked)
		{
			holder.btnSelect.setText(R.string.CANCEL);
			holder.btnSelect.setBackgroundResource(R.drawable.mydate_ok);
		}
		else
		{
			holder.btnSelect.setText(R.string.label_select);
			holder.btnSelect.setBackgroundResource(R.drawable.mydate_cancel);
		}

		holder.txtTitle.setText("[" +  entity.CODE + "]" + entity.SHORTDESCRIPTION);
		holder.txtPrice.setText(entity.PIECE_PRICE + "");
		holder.future = pool.submit(holder);
		return convertView;
	}



	/* (non-Javadoc)
	 * @see android.widget.BaseAdapter#unregisterDataSetObserver(android.database.DataSetObserver)
	 */
	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		if (observer != null) {       
			super.unregisterDataSetObserver(observer);   
		}  
	}

	public class ViewHolder extends Handler implements Runnable
	{
		TextView txtTitle,txtPrice;
		Button btnSelect;
		ImageView btnImage;
		ProductEntity entity;
		Future<?> future;
		
		

		@Override
		public void handleMessage(Message msg)
		{
			if(msg != null && msg.obj != null)
			{
				SoftReference<Bitmap> bitmap = new SoftReference<Bitmap>((Bitmap)msg.obj);
				btnImage.setImageBitmap(bitmap.get());
			}
		}


		@Override
		public void run()
		{

			//获取该产品的第一张照片
			List<ImagesEntity> list = ProductPhotoList.getPhotoInfoList(context, Integer.parseInt(entity.ID));
			for(ImagesEntity image : list)
			{
				if(Utils.Exist(BitmapManager.DIR + "/" + image.getName()))
				{
					Bitmap bitmap = BitmapManager.getResizedImage(BitmapManager.DIR + "/" + image.getName(), 150,150);
					Message msg = new Message();
					msg.obj= bitmap;
					this.sendMessage(msg);
					System.gc();
					break;
				}
			}
			list = null;
		}
	}


}
